package com.mall507.login;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.hzh.frame.widget.xwebview.XWebViewActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.internal.schedulers.NewThreadWorker;

/**
 * 注册第一步
 */
@Route(path = "/login/RegisterFirst")
public class RegisterFirstUI extends BaseUI {

    ImageView agree;
    TextView agreeText;
    Button next;
    TextView send;
    EditText phone;
    EditText code;
    TextView userAgreement;
    TextView privacy;
    private boolean agreecheck = true;
    Disposable disposable;

    @Override
    protected void onCreateBase() {
        setContentView(R.layout.login_ui_user_reg_step1);
        getTitleView().setContent("注册");
        agree = findViewById(R.id.agree);
        agreeText = findViewById(R.id.agreeText);
        next = findViewById(R.id.next);
        send = findViewById(R.id.send);
        phone = findViewById(R.id.phone);
        code = findViewById(R.id.code);
        userAgreement = findViewById(R.id.userAgreement);
        privacy = findViewById(R.id.privacy);
        
        initView();
    }

    public void initView() {
        // 判断是否勾选了同意
        agree.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (agreecheck) {
                    agree.setImageResource(R.mipmap.base_radio_up);
                    agreecheck = false;
                    next.setBackgroundResource(R.drawable.base_button_disabled);
                    next.setClickable(false);
                } else {
                    agree.setImageResource(R.mipmap.base_radio_down);
                    agreecheck = true;
                    next.setBackgroundResource(R.drawable.base_button_select);
                    next.setClickable(true);
                }
            }
        });
        agreeText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in = new Intent(RegisterFirstUI.this, XWebViewActivity.class);
                in.putExtra("common_title", "注册协议");
                in.putExtra("url", "http://www.baidu.com");
                startActivity(in);
            }
        });
        next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!agreecheck) {
                    alert("注册会员时必须同意条款");
                } else if (phone.getText().length() == 0) {
                    alert("请输入手机号");
                } else if (phone.getText().length() != 11) {
                    alert("请输入正确的手机号!");
                } else if (code.getText().length() == 0) {
                    alert("请输入验证码");
                } else {
                    Intent intent = new Intent(RegisterFirstUI.this, RegisterSecondUI.class);
                    intent.putExtra("code", code.getText().toString().trim());
                    intent.putExtra("phone", phone.getText().toString().trim());
                    startActivity(intent);
                }
            }
        });
        send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (phone.getText().length() == 0) {
                    alert("手机号不能为空");
                } else if (phone.getText().length() != 11) {
                    alert("手机号不正确");
                } else {
                    JSONObject params = new JSONObject();
                    try {
                        params.put("phone", phone.getText().toString().trim());
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                    BaseHttp.getInstance().write("login/getSmsCode", params, new HttpCallBack() {
                        @Override
                        public void onSuccess(JSONObject response) {
                            if (200 == response.optInt("code")) {
                                //开启发送计时
                                send.setEnabled(false);
                                send.setBackgroundResource(R.drawable.base_button_disabled);
                                //从0开始发射11个数字为：0-60依次输出，延时0s执行，每1s发射一次。
                                disposable = Flowable.intervalRange(0, 60, 0, 1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).doOnNext(new Consumer<Long>() {
                                    @Override
                                    public void accept(Long aLong) throws Exception {
                                        send.setText((60 - aLong) + "s");
                                    }
                                }).doOnComplete(new Action() {
                                    @Override
                                    public void run() throws Exception {
                                        disposable.dispose();
                                        send.setEnabled(true);
                                        send.setText(RegisterFirstUI.this.getResources().getString(R.string.user_reg_codeinput));
                                        send.setBackgroundResource(R.drawable.base_button_select);
                                    }
                                }).subscribe();
                            } else {
                                alert(response.optString("msg"));
                            }
                        }
                    });
                }
            }
        });
        userAgreement.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final Intent in1 = new Intent(RegisterFirstUI.this, XWebViewActivity.class);
                in1.putExtra("url", "http://admin.yichangyouchuang.cn/api/desc/agreement");
                startActivity(in1);
            }
        });
        privacy.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final Intent in2 = new Intent(RegisterFirstUI.this, XWebViewActivity.class);
                in2.putExtra("url", "http://admin.yichangyouchuang.cn/api/desc/privacy");
                startActivity(in2);
            }
        });
    }
}
